# coding=utf-8
# Time : 2020/10/19 17:16
# Author : huxin
from flask_apscheduler import json

from common.DateTimeUtil import *
from common.YmlUtil import YmlUtil
from RootPath import RootPath
import os
import time


class ActivePower:
    """
    计算有功总电能
    """
    path = os.path.join(RootPath.getRootPath(), "resources/CumulativeData.yml")
    key = "Power"
    para = "active_power"

    def judgeTime(self, startTime,endTime):
        now_time = int(time.strftime("%H%M%S"))
        if now_time >startTime and now_time<endTime:
            return True
        else:
            return False

    def sumPower(self):

        power_vale = YmlUtil.readYml(path=self.path, key=self.key, para=self.para)

        #根据时间段进行判断：
        times = (60000, 210000)
        if self.judgeTime(times[0], times[1]):
            zengliang = 0.5
        else:
            zengliang = 0.2

        #print("增量值是：" + str(zengliang))
        power_vale = float(power_vale) + float(zengliang)
        power_vale = round(power_vale,2)

        #将数据回写
        result = YmlUtil.writeYml(path=self.path, key=self.key, para=self.para, value=power_vale)
        #print(result)

    def readPower(self):

        power_vale = YmlUtil.readYml(path=self.path, key=self.key)
        power_vale["active_power"]=json.dumps(power_vale["active_power"])
        return power_vale
